package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.common.util.CommonUtils;
import cn.icanci.ladybird.common.util.RedisUtils;
import cn.icanci.ladybird.pojo.bo.AdminLoginBO;
import cn.icanci.ladybird.pojo.bo.AdminUpdateBO;
import cn.icanci.ladybird.pojo.entity.Admin;
import cn.icanci.ladybird.service.AdminService;
import cn.icanci.ladybird.service.admin.AdminLoginService;
import cn.icanci.ladybird.service.admin.AdminUpdateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author: icanci
 * @date: Created in 2020/10/8 15:26
 * @classAction: 处理管理员登录注册功能
 */
@Controller
@RequestMapping("/api/v1/pri/admin")
public class AdminController {
    @Autowired
    private AdminLoginService adminLoginService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminDataListController adminDataListController;
    @Autowired
    private AdminUpdateService adminUpdateService;
    @Autowired
    private AdminDataController adminDataController;

    /**
     * 跳转到 login 管理员登录界面
     *
     * @return 返回视图地址
     */
    @RequestMapping("/login")
    public String adminLogin() {
        return "admin/login";
    }

    @PostMapping("/login-up")
    public String login(HttpServletRequest request, HttpServletResponse response, AdminLoginBO adminLoginBO) throws IOException {
        String token = adminLoginService.queryByAdminLoginBO(request, response, adminLoginBO);
        if (StringUtils.isBlank(token)) {
            return adminLogin();
        } else {
            // 向 session 设置 token
            request.getSession().setAttribute("token", token);
            return "admin/index";
        }
    }

    /**
     * 退出登录
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/logout")
    public String adminLogOut(Model model) {
        // 清理 token
        return "admin/login";
    }


    /**
     * 编辑
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/edit")
    public String adminEdit(Model model, Long adminId, Integer pageNum, Integer pageSize) {
        Admin admin = adminService.queryById(adminId);
        ArrayList<Object> list = new ArrayList<>();
        list.add(admin);
        list.add(pageNum);
        list.add(pageSize);
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        // 清理 token
        return "admin/admin-edit";
    }


    /**
     * 更新
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/update")
    public String adminUpdate(Model model, AdminUpdateBO adminUpdateBO) {
        JsonData jsonData = adminUpdateService.adminUpdate(adminUpdateBO);
        if (null != jsonData && jsonData.getCode() == -1) {
            return adminDataController.admin500(model);
        }
        return adminDataListController.adminList(model, adminUpdateBO.getPageNum(), adminUpdateBO.getPageSize());
    }
}
